package at.ac.tuwien.ifs.calendit.basis.gui;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import at.ac.tuwien.ifs.calendit.basis.helper.Constants;
import at.ac.tuwien.ifs.calendit.dao.IMitarbeiter;

public class LoginTableModel {

	private static Log log = LogFactory.getLog(LoginTableModel.class);
	private IMitarbeiter ma;

	/**
	 * The to-be-edited Mitarbeiter.
	 */

	/**
	 * The XML Bean Factory from Spring.
	 */
	private XmlBeanFactory xbf;
	
	public LoginTableModel(){
		
		
	}
	
	public boolean checkLogin(int id, String pw){
		log.info("checking if user typed in correct ID / PW combi");
		ClassPathResource res = new ClassPathResource(Constants.SPRINGBEANS);
		xbf = new XmlBeanFactory(res);
		ma = (IMitarbeiter) xbf.getBean("MitarbeiterDAO");
		boolean correct = true;
		String givenPW = pw;
		String correctPW = ma.login(id);
		if (givenPW.equals(correctPW)){
			correct = true;
			log.debug("user typed in correct comination of ID and pw: "+id+" and "+pw);
		} else {
			correct = false;
			log.debug("user typed in wrong combination of ID and pw: "+id+" and "+pw
					+"does not match");
		}
		return correct;
	}
	
	public int checkRechte(int id){
		log.info("getting \"rechte\" of MA");
		int rechte = ma.getRechte(id);
		return rechte;
	}
}
